﻿#include "Common.h"

void printPositionAttitude(const osg::Matrix& mat)
{
	std::cout << "=== 矩阵分解 ===" << std::endl;


	// 提取平移部分
	osg::Vec3d translation = mat.getTrans();
	std::cout << "平移: " << translation.x() << ", "
		<< translation.y() << ", " << translation.z() << std::endl;


	// 提取旋转部分（四元数）
	osg::Quat rotation = mat.getRotate();
	std::cout << "旋转四元数: " << rotation.x() << ", "
		<< rotation.y() << ", " << rotation.z() << ", " << rotation.w() << std::endl;


	// 转换为欧拉角
	double qAngle;
	osg::Quat::value_type x, y, z;
	rotation.getRotate(qAngle, x, y, z);
	std::cout << "qAngle=" << osg::RadiansToDegrees(qAngle) << " vec: " << x << ", " << y << ", " << z << std::endl;

}

osgViewer::Viewer* getViewerFromNodeVisitor(osg::NodeVisitor* nv)
{
	osgViewer::Viewer* viewer = nullptr;
	osgGA::EventVisitor* ev = dynamic_cast<osgGA::EventVisitor*>(nv);
	if (ev)
	{
		// 获取关联的 View
		osgViewer::View* view = dynamic_cast<osgViewer::View*>(ev->getActionAdapter());
		if (view)
		{
			// 获取 Viewer（兼容 Viewer 和 CompositeViewer）
			osgViewer::ViewerBase* viewerbase = view->getViewerBase();
			viewer = dynamic_cast<osgViewer::Viewer*>(viewerbase);
		}
		else
		{
			std::cout << __FILE__ << "view is null!" << std::endl;
		}
	}
	else
	{
		std::cout << __FILE__ << "node visitor is not event visitor!" << std::endl;
	}

	return viewer;
}
